package com.cxyd.modules.wcs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.Result;
import com.cxyd.modules.wcs.dao.TrkDao;
import com.cxyd.modules.wcs.dto.PositionPathDTO;
import com.cxyd.modules.wcs.dto.TrkDTO;
import com.cxyd.modules.wcs.entity.TrkEntity;
import com.cxyd.modules.wcs.service.TrkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TrkServiceImpl extends CrudServiceImpl<TrkDao, TrkEntity, TrkDTO> implements TrkService {
    @Override
    public QueryWrapper<TrkEntity> getWrapper(Map<String, Object> params) {
        String requestPk = (String) params.get("requestPk");
        String contNo = (String) params.get("contNo");
        String trkType = (String) params.get("trkType");
        String dvcNo = (String) params.get("dvcNo");
        String frmPos = (String) params.get("frmPos");
        String toPos = (String) params.get("toPos");
        QueryWrapper<TrkEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(dvcNo), "dvc_no", dvcNo);
        wrapper.eq(StringUtils.isNotBlank(trkType), "trk_type", trkType);
        wrapper.eq(StringUtils.isNotBlank(requestPk), "pk_id", requestPk);
        wrapper.eq(StringUtils.isNotBlank(frmPos), "frm_pos", frmPos);
        wrapper.eq(StringUtils.isNotBlank(toPos), "to_pos", toPos);
        wrapper.like(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        return wrapper;
    }

    @Override
    public TrkEntity selectByContNo(String contNo) {
        QueryWrapper<TrkEntity> qw = new QueryWrapper<>();
        qw.eq("cont_no", contNo);
        return baseDao.selectOne(qw);
    }

    @Override
    public List<PositionPathDTO> selectPathById(Long trkId) {
        TrkEntity trkEntity = baseDao.selectById(trkId);
        if (trkEntity == null) {
//            根据任务ID未找到相关wcs_trk
            return null;
        }
        List<PositionPathDTO> pathList = new ArrayList<>();
        String frmPos = trkEntity.getFrmPos();
        PositionPathDTO startPath = new PositionPathDTO();
        startPath.setPositionCode(frmPos);
        startPath.setType("05");
        pathList.add(startPath);
        String toPos = trkEntity.getToPos();
        PositionPathDTO endPath = new PositionPathDTO();
        endPath.setPositionCode(toPos);
        endPath.setType("05");
        pathList.add(endPath);
        return pathList;
    }

    @Override
    public void updateTrkByContNo(String contNo) {
//        1.根据托盘信息处理wcs_trk数据
        QueryWrapper<TrkEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(TrkEntity::getContNo, contNo)
                .eq(TrkEntity::getTrkSt, 2);
        TrkEntity trkEntity = baseDao.selectOne(qw);
//        更新上报状态
        trkEntity.setFbSt(1);
        trkEntity.setUpdater(1002L);
        trkEntity.setUpdateDate(new Date());
        baseDao.updateById(trkEntity);
//        2.转移数据至历史表[his_wcs_trk]
        baseDao.backupTrk(contNo);
//        3.删除当前数据
        baseDao.deleteById(trkEntity);
    }

    @Override
    public Result trkHandler(Map<String, Object> params) {
//        接收任务ID已经操作方式(REPT/DONE/CANCEL/DELETE)
        String trkId = (String) params.get("trkId");
        String operateType = (String) params.get("operateType");
        if (StrUtil.isBlankIfStr(trkId)) {
            return new Result<String>().error("任务ID不能为空!");
        }
        if (StrUtil.isBlankIfStr(operateType)) {
            return new Result<String>().error("操作方式不能为空!");
        }
//        根据操作方式进行数据处理
        return null;
    }
}
